package com.bz.xtcx.manager.mapper.provider;

import com.bz.xtcx.manager.entity.CmsExchangesEnroll;
import com.bz.xtcx.manager.persist.conditions.ExchangeEnrollCondition;
import com.bz.xtcx.manager.utils.WzDateUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import org.apache.ibatis.jdbc.SQL;

public class CmsExchangesEnrollProvider {
    public String insertSelectiveProvider(CmsExchangesEnroll cee) {
        return new SQL() {
            {
                INSERT_INTO("cms_exchanges_enroll");
                VALUES("id", "#{id}");
                VALUES("exchanges_id", "#{exchangesId}");
                VALUES("user_id", "#{userId}");
                VALUES("name", "#{name}");
                if (WzStrUtil.isNotBlank(cee.getWorkplace())) {
                    VALUES("workplace", "#{workplace}");
                }
                if (WzStrUtil.isNotBlank(cee.getPhone())) {
                    VALUES("phone", "#{phone}");
                }
                if (WzStrUtil.isNotBlank(cee.getJob())) {
                    VALUES("job", "#{job}");
                }
                if (WzStrUtil.isNotBlank(cee.getEmail())) {
                    VALUES("email", "#{email}");
                }
                if (WzStrUtil.isNotBlank(cee.getAddr())) {
                    VALUES("addr", "#{addr}");
                }
                if (WzStrUtil.isNotBlank(cee.getMemo())) {
                    VALUES("memo", "#{memo}");
                }
                VALUES("enroll_date", "#{enrollDate}");
            }
        }.toString();
    }

    public String countByConditionProvider(ExchangeEnrollCondition eec) {
        StringBuilder sql = new StringBuilder("select count(DISTINCT id) ");
        sql.append("from cms_exchanges_enroll ");
        sql.append("where 1 = 1 ");
        if (WzStrUtil.isNotBlank(eec.getId())) {
            sql.append("and id = '" + eec.getId() + "' ");
        }
        if (WzStrUtil.isNotBlank(eec.getExId())) {
            sql.append("and exchanges_id = '" + eec.getExId() + "' ");
        }
        if (WzStrUtil.isNotBlank(eec.getLoginUserId())) {
            sql.append("and user_id = '" + eec.getLoginUserId() + "' ");
        }
        if (WzStrUtil.isNotBlank(eec.getName())) {
            sql.append("and name like '%" + eec.getName() + "%' ");
        }
        if (null != eec.getEnrollStart()) {
            sql.append("and enroll_date >= '" + WzDateUtil.dt2Str(WzDateUtil.dayStart(eec.getEnrollStart()), WzDateUtil.DT_FMT_5) + "' ");
        }
        if (null != eec.getEnrollEnd()) {
            sql.append("and enroll_date <= '" + WzDateUtil.dt2Str(WzDateUtil.dayEnd(eec.getEnrollEnd()), WzDateUtil.DT_FMT_5) + "' ");
        }
        return sql.toString();
    }

    public String selectByConditionProvider(ExchangeEnrollCondition eec) {

        StringBuilder sql = new StringBuilder("select DISTINCT ");
        sql.append("id, ");
        sql.append("exchanges_id, ");
        sql.append("user_id, ");
        sql.append("name, ");
        sql.append("workplace, ");
        sql.append("job, ");
        sql.append("phone, ");
        sql.append("email, ");
        sql.append("addr, ");
        sql.append("memo, ");
        sql.append("enroll_date ");
        sql.append("from cms_exchanges_enroll ");
        sql.append("where 1 = 1 ");
        if (WzStrUtil.isNotBlank(eec.getId())) {
            sql.append("and id = '" + eec.getId() + "' ");
        }
        if (WzStrUtil.isNotBlank(eec.getExId())) {
            sql.append("and exchanges_id = '" + eec.getExId() + "' ");
        }
        if (WzStrUtil.isNotBlank(eec.getLoginUserId())) {
            sql.append("and user_id = '" + eec.getLoginUserId() + "' ");
        }
        if (WzStrUtil.isNotBlank(eec.getName())) {
            sql.append("and name like '%" + eec.getName() + "%' ");
        }
        if (null != eec.getEnrollStart()) {
            sql.append("and enroll_date >= '" + WzDateUtil.dt2Str(WzDateUtil.dayStart(eec.getEnrollStart()), WzDateUtil.DT_FMT_5) + "' ");
        }
        if (null != eec.getEnrollEnd()) {
            sql.append("and enroll_date <= '" + WzDateUtil.dt2Str(WzDateUtil.dayEnd(eec.getEnrollEnd()), WzDateUtil.DT_FMT_5) + "' ");
        }
        sql.append("order by enroll_date ");
        if (eec.getNeedPaging()) {
            sql.append("limit " + eec.getPageBegin() + ", " + eec.getPageSize());
        }
        return sql.toString();
    }

    public String selectByPrimaryKeyProvider(String id) {
        StringBuilder sql = new StringBuilder("select DISTINCT ");
        sql.append("id, ");
        sql.append("exchanges_id, ");
        sql.append("user_id, ");
        sql.append("name, ");
        sql.append("workplace, ");
        sql.append("job, ");
        sql.append("phone, ");
        sql.append("email, ");
        sql.append("addr, ");
        sql.append("memo, ");
        sql.append("enroll_date ");
        sql.append("from cms_exchanges_enroll ");
        sql.append("where id = '" + id + "' ");
        return sql.toString();
    }
}
